home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Adobe Illustrator Plug-ins / AIQuickCapture Project / source / menuHandler.c next >
Text File  |  1996-06-22  |  7KB  |  319 lines

  1. /* ---------------------------------------------------------------------------
  2.  
  3.     menuHandler.c
  4.  
  5.     Copyright (c) 1995-6 Adobe Systems Incorporated
  6.     All Rights Reserved
  7.     
  8.    --------------------------------------------------------------------------- */
  9.  
  10. // Apple Includes
  11. #include    <QDOffScreen.h>
  12. #include    <Quickdraw.h>
  13. #include    <QuickTimeComponents.h>
  14. #include <Dialogs.h>
  15.  
  16. // Adobe includes
  17. #define __USING_QUICKTIME__
  18. #include "common.h"
  19. #include "menuHandler.h"
  20. #include "aimenugroups.h"
  21.  
  22. #include "raster utils.h"
  23.  
  24.  
  25.  
  26. // defines
  27. #define digitizerDepth        16
  28.  
  29. #define kCaptureDialog 16001
  30. #define kSettingsItem 3
  31. #define kImageItem 4
  32.  
  33. // variables
  34. SeqGrabComponent    theSG;
  35. SGChannel            theVideoChannel;
  36. GWorldPtr            offScreenGWorldPtr;
  37. PixMapHandle        offScreenPMH;
  38. Rect                offScreenVideoRect = {0, 0, 120, 160};
  39.  
  40. WindowPtr            videoWindow;
  41.  
  42.  
  43.  
  44. //functions
  45. Boolean doCaptureDialog( GWorldPtr capturedWorld );
  46. pascal Boolean captureFilterProc(DialogPtr theDialog, EventRecord * theEvent, 
  47.             short * itemHit, long refCon);
  48. OSErr updateImage( DialogPtr theDialog );
  49. OSErr SetupVideo(void);
  50. pascal Boolean SeqGrabberModalFilterProc(DialogPtr theDialog, EventRecord * theEvent, 
  51.             short * itemHit, long refCon);
  52.  
  53.  
  54. // implementation
  55.  
  56. extern FXErr goMenuItem( AIMenuMessage *message ) {
  57.  
  58.     FXErr error = kNoErr;
  59.     Boolean canceled;
  60.     
  61.     if ( message->menuItem == g->quickcamMenu ) {
  62.         Rect rasterRect = { 0, 0, 240, 320 };
  63.         GWorldPtr capturedWorld;
  64.         
  65.         createOffscreen( &capturedWorld, &rasterRect, 32 );    
  66.  
  67.         canceled = doCaptureDialog( capturedWorld );
  68.         
  69.         if ( !canceled ) {    
  70.             AIArtHandle newRaster;
  71.             MakeGWorldTileRequest    request;
  72.     
  73.             // This creates NewArt(kRasterArt...) based on the GWorld supplied.
  74.             error = newRasterArtForGWorld( capturedWorld, _ShortToFixed(72), &request, &newRaster );
  75.             if (error)
  76.                 goto error;
  77.         
  78.             // This sets up all the copy information to copy from a GWorld to RasterArt
  79.             CreateGWorldTile( &request );
  80.     
  81.             // Based on the above two calls, this will copy the GWorld pixels to the RasterArt    
  82.             error = copyGWorldToRasterArt( &request, newRaster );
  83.         
  84.             if ( error ) {
  85.                 if ( newRaster )
  86.                     sArt->DisposeArt( newRaster );
  87.                 }
  88.         }
  89.         disposeOffscreen( &capturedWorld );
  90.     }
  91.  
  92. error:
  93.     return error;
  94.  
  95. }
  96.  
  97.  
  98. Boolean doCaptureDialog( GWorldPtr capturedWorld ) {
  99.     Rect box;
  100.     short item_type;
  101.     Handle pictHandle;
  102.     short hit= ok;
  103.     
  104.     ModalFilterUPP captureFilter =    NewModalFilterProc(captureFilterProc);
  105.     DialogPtr dialog = GetNewDialog( 16001, nil, (WindowPtr) -1 );
  106.  
  107.     if ( dialog ) {
  108.  
  109.         do {
  110.             ModalDialog( captureFilter, &hit);
  111.             if (hit == kSettingsItem ) {
  112.                 SGModalFilterUPP settingsFilter = NewSGModalFilterProc(SeqGrabberModalFilterProc);
  113.  
  114.                 SGSettingsDialog(theSG, theVideoChannel, 0, nil, 0, 
  115.                     settingsFilter, 0);
  116.  
  117.                 DisposeRoutineDescriptor (settingsFilter);
  118.                 }
  119.  
  120.         } while ((hit != ok) && (hit != cancel));
  121.     }
  122.         
  123.     DisposeRoutineDescriptor (captureFilter);
  124.  
  125.     if ( hit == ok ) {
  126.         PixMapHandle pmHandle = GetGWorldPixMap(capturedWorld);;
  127.  
  128.         // grab the capture frame before getting rid of the dialog    
  129.         GetDItem( dialog, kImageItem, &item_type, &pictHandle, &box );
  130.  
  131.         CopyBits( &dialog->portBits, (BitMapPtr)*pmHandle,  
  132.             &box, &capturedWorld->portRect, srcCopy + ditherCopy, nil);
  133.     }
  134.         
  135.     DisposeDialog(dialog);
  136.     
  137.     return ( hit == cancel );
  138. }
  139.  
  140. pascal Boolean captureFilterProc(DialogPtr theDialog, EventRecord * theEvent, 
  141.             short * itemHit, long refCon)
  142. {
  143.     Boolean    handled = false;
  144.  
  145.     handled = CallModalFilterProc(sMacUser->ModalFilterProc, theDialog, theEvent, itemHit );
  146.  
  147.     if ( !handled ) {
  148.         updateImage( theDialog );
  149.     }    
  150.  
  151.     return handled;
  152.  
  153.  
  154.  
  155. OSErr updateImage( DialogPtr theDialog )
  156. {
  157.     OSErr result;
  158.     
  159.     result = SGIdle(theSG);
  160.     if ( !result )
  161.     {
  162.         RGBColor    origForeColor, origBackColor;
  163.         GrafPtr        origPort;
  164.         Rect box;
  165.         short item_type;
  166.         Handle pictHandle;
  167.  
  168.         RGBColor    black = { 0x0000, 0x0000, 0x0000 };
  169.         RGBColor    white = { 0xFFFF, 0xFFFF, 0xFFFF };
  170.  
  171.         GetForeColor( &origForeColor);
  172.         GetBackColor( &origBackColor);
  173.         RGBForeColor(&black);
  174.         RGBBackColor(&white);
  175.         LockPixels(offScreenPMH);
  176.  
  177.         GetDItem( theDialog, kImageItem, &item_type, &pictHandle, &box );
  178.  
  179.         CopyBits((BitMapPtr)*offScreenPMH, &theDialog->portBits,
  180.             &offScreenVideoRect, &box, srcCopy + ditherCopy, nil);
  181.  
  182.         UnlockPixels(offScreenPMH);
  183.         RGBForeColor(&origForeColor);
  184.         RGBBackColor(&origBackColor);
  185.         
  186. //        SetPort ( origPort );
  187.     }
  188.     
  189.     return result;
  190. }
  191.  
  192.  
  193.  
  194.  
  195. OSErr InitializeVideo(void)
  196. {
  197.     OSErr        result;
  198.  
  199.     EnterMovies();
  200.     result = SetupVideo();
  201.     if ( result )
  202.         return result;
  203.  
  204.     return noErr;
  205. }
  206.  
  207. void CloseVideo(void)
  208. {
  209.     if (theVideoChannel != nil)
  210.     {
  211.         SGDisposeChannel(theSG, theVideoChannel);
  212.         theVideoChannel = nil;
  213.     }
  214.     if (theSG != nil)
  215.     {
  216.         CloseComponent(theSG);
  217.         theSG = nil;
  218.     }
  219.     if (offScreenGWorldPtr != nil)
  220.     {
  221.         DisposeGWorld(offScreenGWorldPtr);
  222.         offScreenGWorldPtr = nil;
  223.     }
  224. }
  225.  
  226. OSErr SetupVideo(void)
  227. {
  228.     OSErr    result = noErr;
  229.  
  230.     theSG = OpenDefaultComponent(SeqGrabComponentType, 0);
  231.     if (theSG != nil)
  232.     {
  233.         result = SGInitialize(theSG);
  234.         if ( !result )
  235.         {
  236.             result = SGNewChannel(theSG, VideoMediaType, &theVideoChannel);
  237.             if ( !result )
  238.             {
  239.                 CTabHandle        cTable;
  240.                 
  241.                 result = SGGetSrcVideoBounds( theVideoChannel, &offScreenVideoRect );
  242.                 if ( !result )
  243.                 {
  244.                     cTable = digitizerDepth != 16 && digitizerDepth != 32 ? GetCTable(digitizerDepth) : nil;
  245.                     result = NewGWorld(&offScreenGWorldPtr, digitizerDepth, &offScreenVideoRect, cTable, nil, keepLocal);
  246.                     if ( cTable )
  247.                         DisposeCTable( cTable );
  248.                     if ( !result )
  249.                     {
  250.                         offScreenPMH = GetGWorldPixMap( offScreenGWorldPtr );
  251.                         LockPixels( offScreenPMH );
  252.                         result = SGSetGWorld(theSG, offScreenGWorldPtr, nil);
  253.                         if ( !result )
  254.                         {
  255.                             result = SGSetChannelUsage(theVideoChannel, seqGrabPreview);
  256.                             if ( !result )
  257.                             {
  258.                                 result = SGSetUseScreenBuffer(theVideoChannel, true);
  259.                                 if ( !result )
  260.                                 {
  261.                                     result = SGSetChannelBounds(theVideoChannel, &offScreenVideoRect);
  262.                                     if ( !result )
  263.                                     {
  264.                                         result = SGSetVideoRect(theVideoChannel, &offScreenVideoRect);
  265.                                         if ( !result )
  266.                                             result = SGStartPreview(theSG);
  267.                                     }
  268.                                 }
  269.                             }
  270.                         }
  271.                     }
  272.                 }
  273.             }
  274.         }
  275.     }
  276.  
  277.     return result;
  278. }
  279.  
  280.  
  281.  
  282. //------------------------------
  283. //------------------------------
  284.  
  285.  
  286. pascal Boolean SeqGrabberModalFilterProc(DialogPtr theDialog, EventRecord * theEvent, 
  287.             short * itemHit, long refCon)
  288. {
  289.     Boolean    handled = false;
  290.  
  291.     (void)theDialog;
  292.     if (theEvent->what == updateEvt && (WindowPtr)theEvent->message == (WindowPtr)refCon)
  293.     {
  294.         handled = true;
  295.     }
  296.     return handled;
  297. }
  298.  
  299. //------------------------------
  300. //------------------------------
  301.  
  302. extern FXErr addMyMenus( AIMenuMessage *message ) {
  303.  
  304.     FXErr error = kNoErr;
  305.     PlatformAddMenuItemData throwAwayMenuData;
  306.     
  307.     throwAwayMenuData.groupName = kPlaceMenuGroup;    
  308.     throwAwayMenuData.itemText = g->menuText;
  309.     
  310.     error = sMenu->AddMenuItem( message->d.self, "quickcamHost", &throwAwayMenuData, 
  311.                                 0, &g->quickcamMenu );
  312.  
  313.     if ( error )
  314.         goto error;
  315.  
  316. error:
  317.     return error;
  318. }